package cz.muni.fi.cassandra.Firebrand.entities;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.firebrandocm.dao.annotations.Column;
import org.firebrandocm.dao.annotations.ColumnFamily;
import org.firebrandocm.dao.annotations.Key;
import org.firebrandocm.dao.annotations.MappedCollection;

@ColumnFamily
public class User {

	@Key
	private String id;

	@Column(indexed = true)
	private String name;

	// List of users whom user follows
	@Column(lazy=true)
	@MappedCollection
	private List<User> following;

	// List of users who are following user
	@MappedCollection
	private List<User> followers;

	@MappedCollection
	private List<Tweet> tweets;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<User> getFollowing() {
		if (this.following == null) {
			this.following = new ArrayList<User>();
		}

		return Collections.unmodifiableList(this.following);
	}

	public void setFollowing(List<User> following) {
		this.following = null;

		if (following != null) {
			this.following = new ArrayList<User>();
			this.following.addAll(following);
		}
	}

	public List<User> getFollowers() {
		if (this.followers == null) {
			this.followers = new ArrayList<User>();
		}

		return Collections.unmodifiableList(this.followers);
	}

	public void setFollowers(List<User> followers) {
		this.followers = null;

		if (followers != null) {
			this.followers = new ArrayList<User>();
			this.followers.addAll(followers);
		}
	}

	public List<Tweet> getTweets() {
		if (this.tweets == null) {
			this.tweets = new ArrayList<Tweet>();
		}

		return Collections.unmodifiableList(this.tweets);
	}

	public void setTweets(List<Tweet> tweets) {
		this.tweets = null;

		if (tweets != null) {
			this.tweets = new ArrayList<Tweet>();
			this.tweets.addAll(tweets);
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", following=" + following + ", followers="
				+ followers + ", tweets=" + tweets + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object object) {
		if (!(object instanceof User)) {
			return false;
		}
		User other = (User) object;
		if ((this.id == null) || (this.id != null && !this.id.equals(other.id))) {
			return false;
		}
		return true;
	}

}
